<?php
namespace App\Controllers\Project;

use App\Controllers\BaseController;
use App\Models\Project;

class IndexController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //会员营销项目列表
    //---------------------------------------------------------------------------------
    public function indexAction() {
        $this->display('/project/index/index');
    }

    //---------------------------------------------------------------------------------
    //会员营销项目信息
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('size'),
        ];

        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset  = ($data['page'] - 1) * $data['size'];
        $project = new Project();
        $ret     = $project->gets($offset, $data['size']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }

    //---------------------------------------------------------------------------------
    //会员营销项目总数
    //---------------------------------------------------------------------------------
    public function countAction() {
        $project = new Project();
        $ret     = $project->total();
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }

    public function getkvsAction() {
        $params = $this->dispatcher->getParams();
        $source = ['sms' => 1, 'mms' => 2, 'notice' => 3];
        if(is_array($params) && isset($params[0]) && array_key_exists($params[0], $source)) {
            $filter = $source[$params[0]];
        }
        $project  = new Project();
        $entities = $project->getIdAndName();
        if($entities !== false) {
            if(isset($filter)) {
                foreach($entities as $k => $v) {
                    if($v['info_type'] != $filter) {
                        unset($entities[$k]);
                    }
                }
            }
            foreach($entities as $k => $v) {
                unset($entities[$k]['info_type']);
            }
            return $this->sendAjax(true, $entities);
        }
        return $this->sendAjax(false, '系统错误，请稍后再试！');
    }
}
